package com.neusoft.blog.bean;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

//`loginname` varchar(50) not null comment '登录用户名',
//        `nickname` varchar(50) not null comment '昵称',
//        `password` varchar(100) not null comment '登录密码',
//        `email`   varchar(50) not null comment  '电子邮箱',
//        `regtime` datetime default now() comment '添加时间',
//        `status` tinyint default 1 comment '用户状态 ,1.启用，0禁用',
//        `roles` varchar(50)  comment '用户角色',

//用户表
public class Userinfo implements UserDetails {
    private Integer id;
    private String loginname;
    private String nickname;
    private String password;
    private String email;
    private Date regtime;
    private Boolean status;
    private  Boolean enabled;
    private  Boolean locked;
    private List<Role> roles;

//    public Boolean getEnabled() {
//        return enabled;
//    }


//    public Boolean getLocked() {
//        return locked;
//    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }
    public void setLocked(Boolean locked) {
        this.locked = locked;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLoginname() {
        return loginname;
    }

    public void setLoginname(String loginname) {
        this.loginname = loginname;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //Role XXXX
        List<SimpleGrantedAuthority> authorities=new ArrayList<>();
        for (Role role: roles) {
            authorities.add(new SimpleGrantedAuthority("ROLE_"+role.getName()));
        }
        return authorities;
    }

    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return null;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return !locked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getRegtime() {
        return regtime;
    }

    public void setRegtime(Date regtime) {
        this.regtime = regtime;
    }

    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }


}
